Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,

  • "A man, a plan, a canal: Panama" is a palindrome.
  • "race a car" is not a palindrome.

题目大意:判断给定字符串是不是回文,忽略非数字密码,大小写不敏感

题目难度:Easy

/**
 * Created by gzdaijie on 16/6/12
 */
public class Solution {
    public boolean isPalindrome(String s) {
        char[] str = s.toCharArray();
        int left = 0;
        int right = str.length - 1;

        while (left < right) {
            if (!Character.isAlphabetic(str[left]) && !Character.isDigit(str[left])) {
                left++;
                continue;
            }
            if (!Character.isAlphabetic(str[right]) && !Character.isDigit(str[right])) {
                right--;
                continue;
            }
            if (Character.toLowerCase(str[left]) != Character.toLowerCase(str[right])) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}
gzdaijie            updated 2016-06-12 21:22:09

results matching ""

    No results matching ""